<template>
  <Dialog v-model="adialogDialog" title="导入号码">
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="文本导入" name="call-one"></el-tab-pane>
      <el-tab-pane label="文件导入" name="call-file"></el-tab-pane>
    </el-tabs>
    <el-form ref="formRef" :model="formData" label-width="180px">
      <el-form-item label="号码" required v-if="activeName == 'call-one'">
        <el-input type="textarea" v-model="formData.number" placeholder="请输入号码"></el-input>
      </el-form-item>
      <el-form-item label="上传文件" :required="true" v-if="activeName == 'call-file'">
        <div class="call-box">
          <div class="btn">
            <el-upload
              ref="uploadRef"
              class="upload-box"
              v-model:file-list="fileList"
              :auto-upload="false"
              :limit="1"
              accept=".txt,.xlsx"
              action="none"
            >
              <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
          </div>
          <div class="temp">
            <div class="temp-box temp1" @click="downTxt">模板(txt)</div>
            <div class="temp-box temp2" @click="downTem">模板(xlsx)</div>
          </div>
        </div>
      </el-form-item>
      <div class="tips-box" v-if="activeName == 'call-one'">
        <div class="title">温馨提示</div>
        <div class="content">
          1.可按照换行‘\n’、空格‘ ’、逗号‘，’进行分割，不要添加文字，和其他标点符号。
        </div>
      </div>
      <div class="tips-box" v-if="activeName == 'call-file'">
        <div class="title">温馨提示</div>
        <div class="contet"> 1.xls表格文件的第一行不会被导入 </div>
        <div class="contet">
          2.单次导入名单数量建议不超过10W条,过多容易导入失败:txt格式号码数量暂时未做限制
        </div>
        <div class="contet">
          3.当前导入txt格式只支持号码数据，xlsx支持号码、备注、姓名字段，如需导入带其他客户相关字段,请到客户资料页面操作
        </div>
        <div class="content">
          4.txt文件可按照换行‘\n’、空格‘ ’、逗号‘，’进行分割，不要添加文字，和其他标点符号。
        </div>
      </div>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="adialogDialog = false">取 消</el-button>
    </template>
  </Dialog>
</template>

<script setup>
import { EditPen, Plus } from '@element-plus/icons-vue'
import download from '@/utils/download'
import { RecordApi } from '@/api/call/record'
import * as appendApi from '@/api/call/seating/index'
import calleeListTemplete from '@/views/call/task/callList/calleeListTemplete.txt'
import axios from 'axios'
const activeName = ref('call-one')
const adialogDialog = ref(false)
const fileList = ref([])
const formData = reactive({
  number: null
})
/*显示*/
const open = () => {
  adialogDialog.value = true
  formData.number = null
  fileList.value = []
}
defineExpose({ open })
/*下载模板 xlsx*/
const downTem = async () => {
  const data = await RecordApi.getCallTemplate()
  download.excel(data, '导入呼叫名单模板.xlsx')
}
/*下载模板 txt*/
const downTxt = async () => {
  const response = await axios({
    url: calleeListTemplete,
    responseType: 'blob'
  })
  download.txt(response.data, '呼叫名单模板.txt')
}
/*切换*/
const handleClick = () => {
  formData.number = null
  fileList.value = []
}

const emit = defineEmits(['success'])
const formLoading = ref(false)
/*追加单个号码*/
const appendPhoneOne = async () => {
  const phoneRegex = /^1[3-9]\d{9}$/
  if (!formData.number) {
    ElMessage.error('手机号不能为空')
    return
  }
  try {
    formLoading.value = true
    const formDataUp = new FormData()
    formDataUp.append('numbers', formData.number)

    await appendApi.appendTxt(formDataUp)
    ElMessage.success('导入成功')
    adialogDialog.value = false
    emit('success')
  } finally {
    formLoading.value = false
  }
}
/*追加文件*/
const appendPhoneFile = async () => {
  if (fileList.value.length == 0) {
    ElMessage.error('请上传文件')
    return
  }
  try {
    formLoading.value = true
    const formDataUp = new FormData()
    formDataUp.append('file', fileList.value[0].raw)
    await appendApi.appendFile(formDataUp)
    ElMessage.success('导入成功')
    adialogDialog.value = false
    emit('success')
  } finally {
    formLoading.value = false
  }
}
const submitForm = async () => {
  if (activeName.value == 'call-one') {
    appendPhoneOne()
  } else if (activeName.value == 'call-file') {
    appendPhoneFile()
  }
}
</script>

<style lang="scss" scoped>
.tips-box {
  background-color: #e7f4fd;
  padding: 20px;
  border: 1px solid #dbedfc;
  border-radius: 4px;
  .title {
    font-size: 18px;
    margin-bottom: 5px;
  }
  .contet {
    font-size: 14px;
    margin: 4px 0;
  }
}
.call-box {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .call-select {
    width: calc(100% - 80px);
    padding-left: 10px;
    margin-right: 5px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .call-holder {
    width: calc(100% - 80px);
    padding-left: 10px;
    margin-right: 5px;
    color: #a8abb2;
  }
  .icon-box {
    width: 40px;
    height: 32px;
    background: #f7f7f7;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
  }
  .btn {
    width: calc(100% - 160px);
    display: flex;
    align-items: center;
    justify-content: flex-start;
    .upload-box {
      width: 72px;
      display: flex;
    }
  }
  .temp {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    .temp-box {
      border: 1px solid #e1e6ea;
      background-color: #f8f8f8;
      padding: 2px 5px;
      font-size: 14px;
      cursor: pointer;
    }
    .temp1 {
      border-radius: 4px 0 0 4px;
    }
    .temp2 {
      border-radius: 0 4px 4px 0;
    }
  }
}
::v-deep .el-upload-list {
  margin-left: 10px;
}
::v-deep .el-upload-list__item-file-name {
  width: 100px;
}
</style>
